/**
 * Card Select
 * 
 * Author:        Allen
 * CreateDate:    2014-6-24
 */
(function($) {  
	var settings = {
		multiSelect : true,
		dataLoadPath : null,
		titleField : null,
		valueField : null,
		descriptionField : null,
		itemTemplate : $("<div class='itemclass' id='templateCardItem'><div class='itemclassName'></div><div class='itemclassDes'></div></div>")
	},
	itemData = null,
	selectedData = new Array(),
	$rootPan = null;
	
    $.fn.cardSelect = function(options) {  
    	settings = $.extend(settings,options||{});
		ini(this);
		return {
			//选中所有
			selectAll : selectAll,
			//取消选中所有
			deselectAll : deselectAll,
			//选中指定Id节点
			select : select,
			
			deselect : deselect,
			//获取选中数据
			getSelectedData : getSelectedData,
			//设置选中数据
			setSelectedData : setSelectedData
		};
    };  
    function ini(targetPan){
    	$rootPan = targetPan;
    	load();
	}
   function load(){
	   $.ajax({
			url : settings.dataLoadPath,
			type : "post",
			dataType:'json',
			success : function(text) {
				itemData = text;
				buildCard();
			},
			complete: function(XMLHttpRequest, textStatus){
				activeSelectedData();
			},
			error : function() {
				mini.alert("获取数据出错");
			}
		});
   }
   function buildCard(){
	   $rootPan.empty();
	   var itemTitleFiledName = "this."+settings.titleField;
	   var itemDescriptionFiledName = "this."+settings.descriptionField;
	   var itemValueFiledName = "this."+settings.valueField;
	   
	   var count = 0;
		$(itemData).each(function(){
			var $item = settings.itemTemplate.clone();
			$item.find(".itemclassName").html(eval(itemTitleFiledName));
			$item.find(".itemclassDes").html(eval(itemDescriptionFiledName));
			$item.attr({"id":"cardItem$"+(count++),"value":eval(itemValueFiledName)});
			$rootPan.append($item);
		});		
		
		$(".itemclass").hover(function(){
			$(this).addClass("itemclassHover");
		},function(){
			$(this).removeClass("itemclassHover");
		}).click(function(){
			$this = $(this);
			if($this.hasClass("itemclassSelected")){
				deselect($(this).attr("id"));
			}
			else{
				select($(this).attr("id"));
			}
		});
   }
   function selectAll(){
	   $rootPan.find(".itemclass").each(function(){
		   select($(this).attr("id"));
	   });
   }
   function deselectAll(){
	   $rootPan.find(".itemclass").each(function(){
		   deselect($(this).attr("id"));
	   });
   }
   function select(nodeId){
	   var selectnode = document.getElementById(nodeId);
	   if(selectnode!=null){
		   if(!settings.multiSelect){
			   deselectAll();
		   }
		   var value = $(selectnode).addClass("itemclassSelected").addClass("itemclassSelectedHover").attr("value");
		   addData(value);
	   }
   }
   function deselect(nodeId){
	   var selectnode = document.getElementById(nodeId);
	   if(selectnode!=null){
		   var value = $(selectnode).removeClass("itemclassSelected").removeClass("itemclassSelectedHover").attr("value");
		   removeData(value);
	   }
   }
   //向容器中添加数据
   function addData(data){
	   for(var i=0,length=selectedData.length;i<length;i++){
		   if(selectedData[i]==data)return;
	   }
	   
	   selectedData.push(data);
   }
   //从容器中移除数据
   function removeData(data){
	   for(var i=0,length=selectedData.length;i<length;i++){
		   if(selectedData[i]==data){
			   selectedData.removeAt(i);
		   }
	   }
   }
   function getSelectedData(){
	   var returnData = new Array();
	   for(var i=0,length = selectedData.length;i<length;i++){
		   var currentValue = selectedData[i];
		   for(var j=0,jLength = itemData.length;j<jLength;j++){
			   if(currentValue===eval("itemData[j]."+settings.valueField)){
				   returnData.push(itemData[j]);
			   }
		   }
	   }
	   return {rows:returnData,selectedData:selectedData};
   }
   function setSelectedData(data){
	   if(typeof data.selectedData!="undefined"){
		   selectedData = data.selectedData;
	   }
   }
   function activeSelectedData(){
	   if(selectedData!=null){
		   for(var i=0,length=selectedData.length;i<length;i++){
			   var fitItem = $rootPan.find(".itemclass[value="+selectedData[i]+"]");
			   if((typeof fitItem != "null")&&fitItem.length>0){
				   select($(fitItem[0]).attr("id"));
			   }
		   }
	   }
   }
	
})(jQuery);  